<?php
class MySqlResults {
	
	private $_msgBody;
	private $_numRows;
	private $_numFields;
	private $_lastInsertId;
	private $_sql;
	public $result;
	public $myrow;
	public $ErrorMessages;
	
	function __constructor($strSql) {
		$this->_sql = $strSql;
	}
	
	function _set_msgBody($strMsg) {
		$this->_msgBody .= $strMsg;
	}
	
	function get_msgBody() {
		return $this->_msgBody;
	}
	
	function get_numRows() {
		return $this->_numRows;
	}
	
	function get_numFields() {
		return $this->_numFields;
	}
	
	function get_lastInsertId() {
		$this->_lastInsertId;
	}
	
	private function _raiseError()
	{
		global $SCRIPT_FILENAME;
			global $Browser_Name;
			global $_UserName;
			//$Browser_Name
			
			$subject = "SQL error " . $_SERVER ["HTTP_HOST"];
			$this->_msgBody("<p>
								<b>Script</b>" . $SCRIPT_FILENAME . "<br/>
								<b>Browser</b> : $Browser_Name<br/>
								<b>SQL error</b> : " . mysql_error () . "<br/>
								<b>SQL</b> : " . $this->SQL . "<br/>
								<b>User</b> : $_UserName<br/>									
								<b>Screen</b> : " . $_REQUEST ['SCREEN'] . "<br>");
			
			if (isset ( $_REQUEST ['ACTION'] ))
				$this->_msgBody("<b>Action</b> : " . $_REQUEST ['ACTION'] . "<br>");
				
			$this->_msgBody("<b>Time</b> : " . date ( 'l jS \of F Y h:i:s A' ) . "</p>");
			$tomail = "andy@tabernus.eu";
			$fmail = "error@" . WEB_DOMAIN;
			$fname = "";
			$strHeader = "";
			include (MODULE . "/navigation/EmailHeader");
			SendMime ( $subject, $this->get_msgBody(), $tomail, $fmail, $fname, $strHeader );

			$this->ErrorMessages .= "An alert has been sent to the technical support team.";
			return (FALSE);
	}
	
	public function runSql() 
	{
		$this->result = mysql_query ($this->_sql);

		if($this->result === true)
		{
			$this->_numRows = mysql_num_rows ( $this->result );
			$this->_numFields = mysql_num_fields ( $this->result );
			return true;
		}
		
		if(is_resource($this->result))
		{
			//update delete insert
			if(stristr($this->_sql))
				$this->_lastInsertId = mysql_insert_id();
				
			return true;
		}
		
		if($this->result === false)
		{
			$this->ErrorMessages = mysql_error();
			$this->_raiseError();
			return false;
		}	
	}
}
